<?php

namespace app\model;

use think\facade\Cache;
use think\Model;

class BocDescribe extends Model
{
    protected $table = 'boc_describe';
    /**
     * 查询数据.
     */
//    public function get_audit_product($ctype = '',$title = 'id,cate_id,title,intro,cover,thumb',$audit=1,$order='sort desc,create_at desc'){
//        $list = $this->field($title)->where(['ctype'=>$ctype,'audit'=>$audit])->where(['is_deleted'=>0,'status'=>1])->order($order)->select();
//
//        return $list?$list->toarray():[];
//    }

    /**
     * 上一篇 下一篇.
     */
    public function Page_news($id, $ctype = '')
    {
        $list = self::field('id,title,cate_id,create_at,click_num')->where(['status' => 1, 'is_deleted' => 0, 'cate_id' => $ctype])->order('sort desc,create_at desc')->select()->toarray();

        $kkid = '';
        foreach ($list as $k => $v) {
            if ($v['id'] == $id) {
                $kkid = $k;
                break;
            }
        }

        $data['pre'] = '';
        $data['next'] = '';
        if (!empty($list[$kkid - 1])) {
            $data['pre'] = $list[$kkid - 1];
        }
        if (!empty($list[$kkid + 1])) {
            $data['next'] = $list[$kkid + 1];
        }

        return $data;
    }

    /**
     * 查询数据.
     */
    public function get_limit_product($ctype = '', $audit = 1, $title = 'id,cate_id,title,intro,cover,thumb', $order = 'sort desc,create_at desc', $limit = 4)
    {
        $list = $this->field($title)->where(['ctype' => $ctype, 'audit' => $audit])->where(['is_deleted' => 0, 'status' => 1])->limit($limit)->order($order)->select();

        return $list ? $list->toarray() : [];
    }

    /**
     * 查询数据.
     */
    public function get_product($tag, $ctype = '', $title = 'id,cate_id,title,intro,cover,thumb', $where = ['is_deleted' => 0, 'status' => 1], $order = 'sort desc,create_at desc')
    {
        $data = Cache::get($this->table.'_'.$ctype);

        if (empty($data)) {
            $list = $this->field($title)->where(['ctype' => $ctype])->where($where)->order($order)->select();
            $data = $list ? $list->toarray() : [];
            Cache::tag($tag)->set($this->table.'_'.$ctype, $data);
        }

        return $data;
    }

    /**
     * 查询数据.
     */
    public function get_find_product($tag, $ctype = '', $title = 'id,cate_id,title,intro,cover,thumb', $order = 'sort desc,create_at desc')
    {
        $data = Cache::get($this->table.'_find_'.$ctype);
        if (empty($data)) {
            $list = $this->field($title)->where(['cate_id' => $ctype])->where(['is_deleted' => 0, 'status' => 1, 'audit' => 0])->order($order)->select();
            $data = $list ? $list->toarray() : [];
            Cache::tag($tag)->set($this->table.'_find_'.$ctype, $data);
        }

        return $data;
    }

    public function get_audit_product($tag, $ctype = '', $title = 'id,cate_id,title,intro,cover,thumb', $order = 'sort desc,create_at desc')
    {
        $data = Cache::get($this->table.'_audit_'.$ctype);
        if (empty($data)) {
            $list = $this->field($title)->where(['ctype' => $ctype])->where(['is_deleted' => 0, 'status' => 1, 'audit' => 1])->order($order)->select();
            $data = $list ? $list->toarray() : [];
            Cache::tag($tag)->set($this->table.'_audit_'.$ctype, $data);
        }

        return $data;
    }

    /**
     * 获取详情页随机数据.
     */
    public function Rand_list($id, $type)
    {
        $where = [
            ['id', '<>', $id],
            ['cate_id', '=', $type],
            ['status', '=', '1'],
            ['is_deleted', '=', 0],
        ];

        return $this->field('id,cate_id,title,cover,thumb')->where($where)->limit(3)->order(rand())->select()->toArray();
    }

    /**
     * 查看详情.
     */
    public function get_one($id, $field = 'id,title,thumb,content,left(create_at,10) as create_at')
    {
        $where = [
            ['id', '=', $id],
            ['status', '=', '1'],
            ['is_deleted', '=', 0],
        ];

        $list = $this->field($field)->where($where)->find();

        return $list ? $list->toarray() : [];
    }

    public function page_list($where = ['is_deleted' => 0, 'status' => 1], $field = 'id,title,cover,intro,left(create_at,10) as create_at', $list_rows = 4)
    {
        $list = $this->field($field)->where($where)->order('sort desc,create_at desc')->paginate(['list_rows' => $list_rows, 'query' => request()->param()]);

        return $list;
    }

    public function inc_click($id)
    {
        return $this->where(['id' => $id])->inc('click_num')->update();
    }

    public function get_rand($where = ['is_deleted' => 0, 'status' => 1], $field = 'id,title,cover,left(create_at,10) as create_at', $limit = 4)
    {
        $list = $this->field($field)->where($where)->orderRaw('RAND()')->limit($limit)->select()->toArray();

        return $list;
    }
}